package com.saturn.action.sldb.emergency;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.saturn.app.utils.DateUtils;
import com.saturn.app.web.IAction;
import com.saturn.app.web.IView;
import com.saturn.app.web.view.JspErrorView;
import com.saturn.app.web.view.JspView;
import com.saturn.auth.Organization;
import com.saturn.auth.User;
import com.saturn.sldb.Emergency;
import com.saturn.sldb.PersonState;
import com.saturn.sldb.Reconciliation;
import com.sun.jndi.url.corbaname.corbanameURLContextFactory;

public class ApplyEmergencyAllAction implements IAction {
	@Override
	public IView execute(HttpServletRequest request, HttpServletResponse response) {
		String idStr = request.getParameter("ids");
		String[] ids = idStr.split("__");
		String type = request.getParameter("type"); // 获取要申报的类型
		if ("4".equals(type)) {
			if (ids != null) {
				for (String id : ids) {
					String note = "";
					User user = (User) request.getSession().getAttribute("authUser");
					Emergency emergency = Emergency.get(id);
					User userOragantion = User.getOrganizationsByOrganizationId(user.getId());

					PersonState personState = new PersonState(null, id, user.getId(), user.getName(),
							DateUtils.getSystemTime(), emergency.getName(), emergency.getIdentify(), "", note,
							emergency.getCreaterDepartment(), "临时救助");

					if ("21".equals(userOragantion.getOrganizationId())) {// 如果乡镇街申报状态为待付款、如民政局申报状态为待支付
						emergency.setState("未支付");
						if (emergency.getAudit() == null || "".equals(emergency.getAudit())) {
							return new JspView("/app/sldb/approval/pending/error.jsp");
						} else {
							if (Emergency.editToState(emergency) == 1) {

								personState.setState(emergency.getState());
								PersonState.add(personState);
							}
						}
					} else {
						if ("待付款".equals(Emergency.get(id).getState())) {
							return new JspView("/app/sldb/account/error.jsp");
						} else {
							emergency.setState("待付款");
							if (Emergency.editToState(emergency) == 1) {

								personState.setState(emergency.getState());
								PersonState.add(personState);
								return new JspView("/app/sldb/account/country.jsp");
							}
						}
					}
					continue;
				}
			}
			return new JspView("/app/sldb/approval/pending/show.jsp");
		} else if ("7".equals(type)) {
			if (ids != null) {
				for (String id : ids) {
					String note = "";
					User user = (User) request.getSession().getAttribute("authUser");
					Emergency emergency = Emergency.get(id);
					User userOragantion = User.getOrganizationsByOrganizationId(user.getId());

					PersonState personState = new PersonState(null, id, user.getId(), user.getName(),
							DateUtils.getSystemTime(), emergency.getName(), emergency.getIdentify(), "", note,
							emergency.getCreaterDepartment(), "临时救助");
					emergency.setState("已支付");
					if ("已支付".equals(Emergency.get(id).getState())) {
						return new JspView("/app/sldb/account/error.jsp");
					} else {
						emergency.setSubject("临时专项");
						String subject = emergency.getSubject();
						String organizationId = userOragantion.getOrganizationId();
						Organization organization = Organization.get(organizationId);
						String department = organization.getName();
						String typeStr = "临时救助";
						Reconciliation reg = Reconciliation.getBycondition(subject, department, typeStr);
						if ("".equals(reg) || reg == null) {
							return new JspView("/app/sldb/total/error.jsp");
						} else {
							String money = emergency.getMoney();
							int moneyInt = Integer.parseInt(money) + Integer.parseInt(reg.getSurplus());
							String moneyStr = Integer.toString(moneyInt);
							reg.setSurplus(moneyStr);
							String total = reg.getTotal();
							String surplus = reg.getSurplus();
							int balance = Integer.parseInt(total) - Integer.parseInt(surplus);
							String balanceStr = Integer.toString(balance);
							reg.setBalance(balanceStr);
							if (Emergency.editToState(emergency) == 1) {
								personState.setState(emergency.getState());
								PersonState.add(personState);
								Reconciliation.edit(reg);

							} else {
								return new JspErrorView("信息更新失败");
							}
							continue;
						}
					}
				}
				return new JspView("/app/sldb/account/center.jsp");
			}

		} else if ("13".equals(type)) {
			if (ids != null) {
				for (String id : ids) {
					String note = "";
					User user = (User) request.getSession().getAttribute("authUser");
					Emergency emergency = Emergency.get(id);
					User userOragantion = User.getOrganizationsByOrganizationId(user.getId());

					PersonState personState = new PersonState(null, id, user.getId(), user.getName(),
							DateUtils.getSystemTime(), emergency.getName(), emergency.getIdentify(), "", note,
							emergency.getCreaterDepartment(), "临时救助");
					emergency.setState("已支付");
					if ("已支付".equals(Emergency.get(id).getState())) {
						return new JspView("/app/sldb/account/error.jsp");
					} else {
						emergency.setSubject("结余调剂");
						String subject = emergency.getSubject();
						// String department = emergency.getCreaterDepartment();
						String organizationId = userOragantion.getOrganizationId();
						Organization organization = Organization.get(organizationId);
						String department = organization.getName();
						String typeStr = "临时救助";
						Reconciliation reg = Reconciliation.getBycondition(subject, department, typeStr);
						if ("".equals(reg) || reg == null) {
							return new JspView("/app/sldb/total/error.jsp");
						} else {
							String money = emergency.getMoney();
							int moneyInt = Integer.parseInt(money) + Integer.parseInt(reg.getSurplus());
							String moneyStr = Integer.toString(moneyInt);
							reg.setSurplus(moneyStr);
							String total = reg.getTotal();
							String surplus = reg.getSurplus();
							int balance = Integer.parseInt(total) - Integer.parseInt(surplus);
							String balanceStr = Integer.toString(balance);
							reg.setBalance(balanceStr);
							if (Emergency.editToState(emergency) == 1) {
								personState.setState(emergency.getState());
								PersonState.add(personState);
								Reconciliation.edit(reg);

							} else {
								return new JspErrorView("信息更新失败");
							}
						}
						continue;
					}
				}
				return new JspView("/app/sldb/account/center.jsp");
			}
		}
		return null;
	}

	@Override
	public String requestMapping() {
		return "/app/sldb/emergency/applyEmergencyAll.action";
	}
}
